package com.losdun.points.controller.admin;

import com.losdun.common.core.controller.BaseController;
import com.losdun.common.core.domain.AjaxResult;
import com.losdun.common.core.page.TableDataInfo;
import com.losdun.points.domain.PointsOrder;
import com.losdun.points.enums.PointsOrderStatusEnum;
import com.losdun.points.service.PointsOrderService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description
 * @Author Sword.Ye
 * @Date 2025/7/8
 **/
@RestController
@Slf4j
@RequestMapping(value = "/points/order")
public class PointsOrderController extends BaseController {
    @Autowired
    private PointsOrderService pointsOrderService;

    @PreAuthorize("@ss.hasPermi('points:order:list')")
    @GetMapping("/list")
    @ApiOperation("查询列表")
    public TableDataInfo list(PointsOrder order) {
        startPage();
        List<PointsOrder> list= pointsOrderService.queryList(order);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('points:order:add')")
    @PostMapping("/insert")
    @ApiOperation("新增")
    public AjaxResult insert(@RequestBody PointsOrder order){
        return null;
    }

    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('points:order:edit')")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody PointsOrder order){

        try {
            pointsOrderService.updateById(order);
            return AjaxResult.success("更新成功");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return AjaxResult.error("更新失败！");
        }
    }

    @PreAuthorize("@ss.hasPermi('points:order:shipping')")
    @PostMapping("/shipOrder")
    @ApiOperation("发货")
    public AjaxResult shipOrder(@RequestBody PointsOrder order){

        try {
            PointsOrder old = pointsOrderService.getById(order.getId());
            old.setDeliveryName(order.getDeliveryName());
            old.setDeliveryCode(order.getDeliveryCode());
            old.setDeliveryTime(LocalDateTime.now());
            old.setStatus(PointsOrderStatusEnum.sended.getCode());
            pointsOrderService.updateById(old);
            return AjaxResult.success("更新成功");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return AjaxResult.error("更新失败！");
        }
    }

    @PreAuthorize("@ss.hasPermi('points:order:received')")
    @PostMapping("/receiveOrder")
    @ApiOperation("收货")
    public AjaxResult receiveOrder(Long id){

        try {
            PointsOrder old = pointsOrderService.getById(id);
            old.setStatus(PointsOrderStatusEnum.received.getCode());
            old.setReceivedTime(LocalDateTime.now());
            pointsOrderService.updateById(old);
            return AjaxResult.success("更新成功");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return AjaxResult.error("更新失败！");
        }
    }


    @PreAuthorize("@ss.hasPermi('points:order:remove')")
    @PostMapping("/delete")
    @ApiOperation("删除")
    public AjaxResult delete(Long id){
        try {
            pointsOrderService.removeById(id);
            return AjaxResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return AjaxResult.error("删除失败！");
        }
    }


}
